<?php
session_start();

$institute_id = $_SESSION['institute_id'];
$user_id= $_SESSION['user_id'];
//echo "institute_id=$institute_id<bR>";
if(isset($_POST['delete']) && $_POST['delete'] !='')
{
	//exit;
	$sqltext="select saved_file_name, file_name, file_type
										from docs WHERE id=?";
	$params = array();	
	array_push($params,sql_escape($_POST['del_id']));
	$result = db_select_query($conn,$sqltext,$params);
	while($row = db_fetch_object($result))
	{
		foreach ($row as $key => $value) 
		{
			$value = trim($value);
			$$key = $value;
		}	
	}
	$target = $_SERVER['DOCUMENT_ROOT'] . '/math-videos/docs/'.$institute_id.'/'. $asset_id."/";
	if(file_exists($target.$saved_file_name.".".$file_type))
	{
		unlink($target.$saved_file_name.".".$file_type);
	}
	$sqltext="delete from docs where id=?";
	$params = array();
	array_push($params,sql_escape($_POST['del_id']));
	db_change_query($conn,$sqltext,$params);	

}
if(isset($_POST['save']) && $_POST['save'] !='')
{
	$file = basename( $_FILES['upload_file']['name']);
	$error_message ='';
	if(!isset($file) ||  $file=='')
	{
		$error_message .= "missing required field - file";
	}
	if($error_message  =='')
	{
	 $sqltext = "SELECT id FROM docs ORDER BY id DESC LIMIT 1";
		$params = array();
    $result = db_select_query($conn,$sqltext,$params);
		while($row = db_fetch_object($result))
		{
			foreach ($row as $key => $value) 
			{
				$value = trim($value);
				$$key= $value;
			}
		}
		$new_id = $new_id+1;
		$path_parts = pathinfo($file);
		$file_type  = $path_parts['extension'];
		$file_name  = $path_parts['filename'];
		
		$saved_file_name = $file_name."_".$asset_id."_".$new_id;		
		
		$sqltext="INSERT INTO docs(asset_id, institute_id,file_name, file_type,saved_file_name, created_by, created_date) values( ?,?,?,?,?,?,?)";
		$params = array();
		array_push($params,sql_escape($asset_id));
		array_push($params,sql_escape($institute_id));
		array_push($params,sql_escape($file_name));
		array_push($params,sql_escape($file_type));
		array_push($params,sql_escape($saved_file_name));
		array_push($params,sql_escape($user_id));
		array_push($params,date("Y-m-d H:i:s"));
		
		db_change_query($conn,$sqltext,$params);	
	//	if($file != '')
		//{	
			if(!is_dir($_SERVER['DOCUMENT_ROOT']."/math-videos/docs/$institute_id"))
			{
				mkdir($_SERVER['DOCUMENT_ROOT']."/math-videos/docs/$institute_id", 0777); 
				mkdir($_SERVER['DOCUMENT_ROOT']."/math-videos/docs/$institute_id/$asset_id", 0777); 
			}
			elseif(!is_dir($_SERVER['DOCUMENT_ROOT']."/math-videos/docs/$institute_id/$asset_id"))
			{
				mkdir($_SERVER['DOCUMENT_ROOT']."/math-videos/docs/$institute_id/$asset_id", 0777); 
			}
			$target = $_SERVER['DOCUMENT_ROOT'] . '/math-videos/docs/'.$institute_id.'/'. $asset_id."/";
			/*if(file_exists($target.$saved_file_name.".".$file_type))
			{
				unlink($target.$saved_file_name.".".$file_type);
			}
		*/
			if(!move_uploaded_file($_FILES["upload_file"]["tmp_name"],$target.$saved_file_name.".".$file_type)) 
			{
				 echo "<div  style='color:red;' align='center'> Sorry, there was a problem uploading your file.</div>";
			}
			chmod($target.$saved_file_name.".".$file_type, 0755);
		//}
	}
		
	else
	{
		$alert_message = $error_message;
	}
}
$docs_area .="";
if($asset_id !='')
{
 	$asset_id= $_REQUEST['asset_id'];
	$sqltext="select saved_file_name, file_name, file_type,id
										from docs WHERE asset_id=?";
	$params = array();	
	array_push($params,sql_escape($asset_id));
	$result = db_select_query($conn,$sqltext,$params);
	$docs_area .="<br /><b>List all docs for this asset:<br/></b><br/><table border='1' cellpadding='5' cellspacing='1'><tr><th>File Name</th><th>&nbsp;</th></tr>";
	$has_flag= false;
	while($row = db_fetch_object($result))
	{
		$has_flag= true;
		foreach ($row as $key => $value) 
		{
			$value = trim($value);
			$$key = $value;
		}	
		$docs_area .="<tr><td><a href=\"../../docs/".$institute_id."/".$asset_id."/".$saved_file_name.".".$file_type."'\">". $file_name.".".$file_type."</a></td><td>
		<input type='submit' name='delete' value='Remove' onclick='this.form.del_id.value=".$id."' /></td></tr>";
	}
	if(!$has_flag)
	{
		$docs_area .="<tr><td colspan='9'>No docs found for this asset!</td></tr>";
	}
	$docs_area .="</table>";
}

$docs_area .="<input type='hidden' name='del_id' value=\"\" /><br/><input type='hidden' name='asset_id' value=\"$asset_id\" />
<b>Add new docs:<br /></b><hr style='border-color:#999;'/><br/><table>";

$docs_area .="
<tr><td>File:</td><td><input  type=\"file\" name=\"upload_file\"  size='60' /></td></tr>
";
$docs_area .="<tr><td colspan='2' align='right'><input type='submit' name='save' value='Upload Docs'></td></tr>";
$docs_area .="
</table>";
?>